package shape2D;

import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.scene.layout.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.collections.ObservableList;

// 折线(或者用效果里的外观转换)      
public class PolygonDemo extends Application {
	private Pane root = new Pane();
	private Polygon polygon;
	private Circle leftWheel;
	private Circle rightWheel;
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage stage) {
		
		// 一系列的点按顺序连接成为闭合折线，改成Polyline即为折线段
		polygon = new Polygon();
		polygon.setFill(Color.LIGHTGREEN);	// 注意此处若设置为null,则鼠标事件点击内部无反应
		
		// 放折线的点的可观察列表
		ObservableList<Double> List = polygon.getPoints();
		
		// 按顺序加入点构成车子
		List.add(10.0);  List.add(100.0);
		List.add(100.0); List.add(100.0);
		
		List.add(100.0); List.add(85.0);
		List.add(70.0);  List.add(85.0);
		
		List.add(60.0); List.add(70.0);
		List.add(35.0); List.add(70.0);
		
		List.add(25.0); List.add(85.0);
		List.add(10.0); List.add(85.0);
		
		leftWheel = new Circle(30, 107, 7);		// 左轮子
		rightWheel = new Circle(70, 107, 7);	// 右轮子
		root.getChildren().addAll(polygon, leftWheel, rightWheel);
				
		Scene scene = new Scene(root, 300, 200);
		stage.setScene(scene);
		stage.setTitle("Polygon");
		stage.show();
		
		// 动画
		TranslateTransition moveCar = new TranslateTransition(Duration.millis(3000), polygon);
		moveCar.setFromX(0); moveCar.setFromY(0);     // 以此时所在位置为原点
		moveCar.setToX(100); moveCar.setToY(0);
		moveCar.setCycleCount(Timeline.INDEFINITE);
		
		TranslateTransition moveLeftWheel = new TranslateTransition(Duration.millis(3000), leftWheel);
		moveLeftWheel.setFromX(0); moveLeftWheel.setFromY(0);    
		moveLeftWheel.setToX(100); moveLeftWheel.setToY(0);
		moveLeftWheel.setCycleCount(Timeline.INDEFINITE);
		
		TranslateTransition moveRightWheel = new TranslateTransition(Duration.millis(3000), rightWheel);
		moveRightWheel.setFromX(0); moveRightWheel.setFromY(0);    
		moveRightWheel.setToX(100); moveRightWheel.setToY(0);
		moveRightWheel.setCycleCount(Timeline.INDEFINITE);
		
		ParallelTransition parat = new ParallelTransition(moveCar, moveLeftWheel, moveRightWheel);
		parat.play();
	}
}




